home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5432 < prev    next >
Encoding:
Text File  |  1996-08-05  |  11.1 KB  |  433 lines

  1. Path: news.uoregon.edu!xmission!news
  2. From: tknarr@xmission.com  ( Todd Knarr )
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Date class
  5. Date: 4 Feb 1996 18:03:04 GMT
  6. Organization: Chaos Central
  7. Message-ID: <4f2sco$bct@news.xmission.com>
  8. References: <4f09k2$nnu@george.rutgers.edu>
  9. Reply-To: tknarr@xmission.com ( Todd Knarr )
  10. NNTP-Posting-Host: slc68.xmission.com
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4f09k2$nnu@george.rutgers.edu>, aminabdu@george.rutgers.edu (Amin Abdulghani) writes:
  14. >I am trying to build a small prototype which involves lot of manipulation with
  15. >dates i.e. incrementing/decrementing dates by a week, month and  so on. Is
  16. >anyone aware of any available classes which already do such manipulations. Any
  17. >pointers would be helpful.
  18.  
  19. I don't know if this is exactly what you are looking for, but
  20. this is a class I came up with for handling dates. The arithmetic
  21. deals with adding/subtracting days to/from a date, so you'd either
  22. have to have a table of days per month or convert to YMD, add one
  23. to M and convert back to a Date. The << and >> operators aren't
  24. included, mostly because thanks to the code this has to fit into I
  25. can't use << and >>.
  26.  
  27. #ifndef CLASS_DATE_H
  28. #define CLASS_DATE_H
  29.  
  30. //
  31. //  Classname : Date
  32. //
  33. //  Author    : Todd Knarr
  34. //
  35. //  Description    :
  36. //      Provides a Date class which represents dates as Julian day numbers
  37. //      ( days since 1 Jan 4713 BC ).
  38. //      This class can handle all dates from  1 Jan 4713BC to 31 Dec 9999AD.
  39. //
  40. //      Note: Years AD are positive, years BC are negative. There is
  41. //      no year 0AD, it goes from 1BC to 1AD. A year of 0 will be treated
  42. //      as 1BC. No attempt is made to validate ranges. Physical errors
  43. //    will not result from insane day-month combinations like the 87th
  44. //    day of the 45th month, but the results will obviously not make
  45. //    much sense.
  46. //
  47. //      Date conversion routines by Eric Bergman-Terrell,
  48. //          Computer Language Dec 1990.
  49. //
  50.  
  51. #include <stdlib.h>
  52. #include <time.h>
  53.  
  54. class Date
  55. {
  56.  
  57. private:
  58.  
  59.     long        lJulianDay;
  60.  
  61.     //
  62.     //  Function :      YmdToJd
  63.     //
  64.     //  Parameters     : int year, month, day
  65.     //
  66.     //  Return values  : long julian day
  67.     //
  68.     //  Description    : converts year/month/day to julian day
  69.     //
  70.     static long YmdToJd( const int iYear, const int iMonth, const int iDay );
  71.  
  72.     //
  73.     //  Function :      JdToYmd
  74.     //
  75.     //  Parameters     : long julian day, pointers to int year, month, day
  76.     //
  77.     //  Return values  : none
  78.     //
  79.     //  Description    : converts julian day to year/month/day
  80.     //
  81.     static void JdToYmd( const long lJD, int *piYear, int *piMonth,
  82.                          int *piDay );
  83.  
  84.     //
  85.     //  Function :      Addition, Subtraction helper functions
  86.     //
  87.     //  Parameters     :
  88.     //
  89.     //  Return values  :
  90.     //
  91.     //  Description    : Adds days to current Date, subtracts days from
  92.     //          current Date, takes difference of two Dates in days.
  93.     //
  94.     Date& Add( const long iDays )
  95.     {
  96.         lJulianDay += iDays;
  97.         return *this;
  98.     }
  99.     Date& Subtract( const long iDays )
  100.     {
  101.         lJulianDay -= iDays;
  102.         return *this;
  103.     }
  104.     long Subtract( const Date Other ) const
  105.     {
  106.         return lJulianDay - Other.lJulianDay;
  107.     }
  108.  
  109. public:
  110.  
  111.     //
  112.     //  Function :      IsLeapYear
  113.     //
  114.     //  Parameters     : int year
  115.     //
  116.     //  Return values  : int
  117.     //
  118.     //  Description    : returns 1 if the given year is a leap year,
  119.     //                   0 otherwise
  120.     //
  121.     static int IsLeapYear( const int iYear )
  122.     {
  123.         long jd1, jd2;
  124.         jd1 = YmdToJd( iYear, 2, 29 );
  125.         jd2 = YmdToJd( iYear, 3, 1 );
  126.         return (int) ( jd2 - jd1 );
  127.     }
  128.  
  129.     //
  130.     //  Function :      default constructor
  131.     //
  132.     //  Parameters     : none
  133.     //
  134.     //  Return values  : none
  135.     //
  136.     //  Description    : constructs a new object initialized to 1 Jan 4713BC
  137.     //
  138.     Date() { lJulianDay = 0L; }
  139.  
  140.     //
  141.     //  Function :      time_t constructor
  142.     //
  143.     //  Parameters     : none
  144.     //
  145.     //  Return values  : none
  146.     //
  147.     //  Description    : constructs an object initialized to the date
  148.     //                   represented by a system time value.
  149.     //
  150.     Date( const time_t tSysTime )
  151.     {
  152.         struct tm *ptm;
  153.  
  154.         ptm = localtime( &tSysTime );
  155.         lJulianDay = YmdToJd( ptm->tm_year, ptm->tm_mon, ptm->tm_mday );
  156.     }
  157.  
  158.     //
  159.     //  Function :      char* ( string ) constructor
  160.     //
  161.     //  Parameters     :
  162.     //
  163.     //  Return values  :
  164.     //
  165.     //  Description    : constructs an object from a string.
  166.     //          The string is formatted as the ASCII representation
  167.     //          of the long Julian day number.
  168.     //
  169.     Date( const char *String )
  170.     {
  171.         lJulianDay = atol( String );
  172.     }
  173.  
  174.     //
  175.     //  Function :      year/month/day constructor
  176.     //
  177.     //  Parameters     : int year, month, day
  178.     //
  179.     //  Return values  : none
  180.     //
  181.     //  Description    : constructs an object initialized to
  182.     //          the date given by the arguments
  183.     //
  184.     Date( const int iDay, const int iMonth, const int iYear )
  185.     {
  186.         lJulianDay = YmdToJd( iYear, iMonth, iDay );
  187.     }
  188.  
  189.     //
  190.     //  Function :      Year, Month, Day, DayOfYear, DayOfWeek, IsLeapYear, YMD
  191.     //
  192.     //  Parameters     : none
  193.     //
  194.     //  Return values  : int year, month or day
  195.     //
  196.     //  Description    : Returns the year, month or day corresponding
  197.     //          to the date, the day of the year and the day of the
  198.     //          week matching what localtime() gives ( 0 = Sunday,
  199.     //          6 = Saturday ). YMD() produces the year, month and day
  200.     //          of the object in one operation.
  201.     //
  202.     int Year( void ) const
  203.     {
  204.         int y, m, d;
  205.  
  206.         JdToYmd( lJulianDay, &y, &m, &d );
  207.         return y;
  208.     }
  209.     int Month( void ) const
  210.     {
  211.         int y, m, d;
  212.  
  213.         JdToYmd( lJulianDay, &y, &m, &d );
  214.         return m;
  215.     }
  216.     int Day( void ) const
  217.     {
  218.         int y, m, d;
  219.  
  220.         JdToYmd( lJulianDay, &y, &m, &d );
  221.         return d;
  222.     }
  223.     int DayOfYear( void ) const;
  224.     int DayOfWeek( void ) const
  225.     {
  226.         return ( ( ( (int) ( lJulianDay % 7L ) ) + 1 ) % 7 );
  227.     }
  228.     int IsLeapYear( void ) const
  229.     {
  230.         int y, m, d;
  231.         JdToYmd( lJulianDay, &y, &m, &d );
  232.         return IsLeapYear( y );
  233.     }
  234.     void YMD( int *pY, int *pM, int *pD )
  235.     {
  236.         JdToYmd( lJulianDay, pY, pM, pD );
  237.         return;
  238.     }
  239.  
  240.     //
  241.     //  Function :      Addition operators
  242.     //
  243.     //  Parameters     :
  244.     //
  245.     //  Return values  :
  246.     //
  247.     //  Description    :  Adding an integral type to a Date adds
  248.     //          the number of days in the integral type to the Date.
  249.     //          Subtracting an integral type from a date subtracts the
  250.     //          number of days in the integral type from the Date.
  251.     //          Subtracting two Dates gives the number of days between
  252.     //          them as a long. Adding two Dates is not defined.
  253.     //
  254.     friend Date operator+( const Date& Left, const long Right )
  255.     {
  256.         Date Temp = Left;
  257.         Temp.Add( Right );
  258.         return Temp;
  259.     }
  260.     friend Date operator+( const long Left, const Date& Right )
  261.     {
  262.         Date Temp = Right;
  263.         Temp.Add( Left );
  264.         return Temp;
  265.     }
  266.     Date& operator+=( const long Right )
  267.     {
  268.         Add( Right );
  269.         return *this;
  270.     }
  271.     friend Date operator-( const Date& Left, const long Right )
  272.     {
  273.         Date Temp = Left;
  274.         Temp.Subtract( Right );
  275.         return Temp;
  276.     }
  277.     friend Date operator-( const long Left, const Date& Right )
  278.     {
  279.         Date Temp = Right;
  280.         Temp.Subtract( Left );
  281.         return Temp;
  282.     }
  283.     Date& operator-=( const long Right )
  284.     {
  285.         Subtract( Right );
  286.         return *this;
  287.     }
  288.     friend Date operator+( const Date& Left, const unsigned long Right )
  289.     {
  290.         Date Temp = Left;
  291.         Temp.Add( Right );
  292.         return Temp;
  293.     }
  294.     friend Date operator+( const unsigned long Left, const Date& Right )
  295.     {
  296.         Date Temp = Right;
  297.         Temp.Add( Left );
  298.         return Temp;
  299.     }
  300.     Date& operator+=( const unsigned long Right )
  301.     {
  302.         Add( (long) Right );
  303.         return *this;
  304.     }
  305.     friend Date operator-( const Date& Left, const unsigned long Right )
  306.     {
  307.         Date Temp = Left;
  308.         Temp.Subtract( (long) Right );
  309.         return Temp;
  310.     }
  311.     friend Date operator-( const unsigned long Left, const Date& Right )
  312.     {
  313.         Date Temp = Right;
  314.         Temp.Subtract( (long) Left );
  315.         return Temp;
  316.     }
  317.     Date& operator-=( const unsigned long Right )
  318.     {
  319.         Subtract( (long) Right );
  320.         return *this;
  321.     }
  322.     long operator-( const Date& Right )
  323.     {
  324.         return lJulianDay - Right.lJulianDay;
  325.     }
  326.  
  327.     //
  328.     //  Function :      ++ and -- operators, prefix and postfix forms
  329.     //
  330.     //  Parameters     :
  331.     //
  332.     //  Return values  :
  333.     //
  334.     //  Description    :
  335.     //          Increment or decrement a Date by 1 day. The standard
  336.     //          semantics for prefix/postfix apply.
  337.     //
  338.     Date& operator++()
  339.     {
  340.         lJulianDay++;
  341.         return *this;
  342.     }
  343.     Date operator++( int )
  344.     {
  345.         Date Temp = *this;
  346.         lJulianDay++;
  347.         return Temp;
  348.     }
  349.     Date& operator--()
  350.     {
  351.         lJulianDay++;
  352.         return *this;
  353.     }
  354.     Date operator--( int )
  355.     {
  356.         Date Temp = *this;
  357.         lJulianDay++;
  358.         return Temp;
  359.     }
  360.  
  361.     //
  362.     //  Function :      ToString
  363.     //
  364.     //  Parameters     : pointer to string buffer to fill in
  365.     //
  366.     //  Return values  : none
  367.     //
  368.     //  Description    : Formats the Date into an ASCII representation.
  369.     //          This is the ASCII form of the long Julian day number.
  370.     //          The string is a fixed-length 12-character string, including
  371.     //          the NUL terminator;
  372.     //
  373.     void ToString( char *szBuffer ) const;
  374.  
  375.     //
  376.     //  Function :      comparison operators
  377.     //
  378.     //  Parameters     :
  379.     //
  380.     //  Return values  :
  381.     //
  382.     //  Description    :
  383.     //
  384.     int operator==( const Date& Right ) const
  385.     {
  386.         return lJulianDay == Right.lJulianDay;
  387.     }
  388.     int operator!=( const Date& Right ) const
  389.     {
  390.         return lJulianDay != Right.lJulianDay;
  391.     }
  392.     int operator<( const Date& Right ) const
  393.     {
  394.         return lJulianDay < Right.lJulianDay;
  395.     }
  396.     int operator<=( const Date& Right ) const
  397.     {
  398.         return lJulianDay <= Right.lJulianDay;
  399.     }
  400.     int operator>( const Date& Right ) const
  401.     {
  402.         return lJulianDay > Right.lJulianDay;
  403.     }
  404.     int operator>=( const Date& Right ) const
  405.     {
  406.         return lJulianDay >= Right.lJulianDay;
  407.     }
  408.  
  409.     //
  410.     //  Function :      ToSysTime
  411.     //
  412.     //  Parameters     : none
  413.     //
  414.     //  Return values  : converted result
  415.     //
  416.     //  Description    : Converts the date to a time_t value
  417.     //          representing midnight of that date.
  418.     //
  419.     time_t ToSysTime( void ) const;
  420.  
  421. };
  422.  
  423. #endif
  424.  
  425. --
  426. Todd Knarr : tknarr@xmission.com      |  finger for PGP public key
  427.                                       |  Member, USENET Cabal
  428.  
  429. Seriously, I don't want to die just yet.  I don't care how
  430. good-looking they are, I! don't! want! to! die!"
  431.                                         -- Megazone ( UF1 )
  432.  
  433.